package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.Arc;
import com.efast.dmom.mes.bean.Route;
import com.efast.dmom.mes.bean.RouteStep;
import com.efast.dmom.mes.bean.WorkCenter;
import com.efast.dmom.mes.dao.entity.RouteStepDao;
import com.efast.dmom.mes.entity.RouteEntity;
import com.efast.dmom.mes.entity.RouteStepEntity;
import com.efast.dmom.mes.entity.base.IModificationFlags;
import com.efast.dmom.mes.util.TimeUtil;

/**
 * @author Aaron Liu
 *
 * @date:2020年2月4日
 */
@Component
public class RouteStepDataHandler {

	@Autowired
	private RouteStepDao routeStepDao = null;

	@Autowired
	private RouteArcDataHandler arcDataHandler = null;

	@Autowired
	private RouteStepWCRelDataHandler routeStepWCRelDataHandler = null;

	protected List<RouteStep> saveRouteSteps(Route route, List<RouteStep> stepList) {

		if (route == null || stepList == null) {
			return null;
		}
		
		RouteEntity routeEntity = route.getInnerData();

		if (IModificationFlags.UNCHANGED == route.getModificationFlag()) {
			return route.getRouteSteps();
		}

		List<RouteStep> deletedList = new ArrayList<RouteStep>();
		List<RouteStep> modifiedList = new ArrayList<RouteStep>();
		List<RouteStep> createdList = new ArrayList<RouteStep>();
		
		for (RouteStep step : stepList) {

			int modificationFlag = step.getModificationFlag();
			
			RouteStepEntity innerData = step.getInnerData();

			if (IModificationFlags.NEW == modificationFlag) {
				
				innerData.setRouteKey(routeEntity.getId());
				innerData.setSiteId(DataHandlerUtils.getSiteId());
				innerData.setCreationTime(TimeUtil.getLocalDateTime());
				innerData.setCreationTimeZone(TimeUtil.getLocalTimeZone());
				innerData.setCreator(SpringWebUtils.getRequestUser());
				innerData.setLastModifiedTime(TimeUtil.getLocalDateTime());
				innerData.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
				innerData.setLastModifier(SpringWebUtils.getRequestUser());
//				innerData.setCreationPid(DataHandlerUtils.getDmomPid());
//				innerData.setLastModifiedPid(DataHandlerUtils.getDmomPid());
				innerData.setRowStatus(1);
				innerData.setRowTrxId(1);

				step.setInnerData(innerData);
				createdList.add(step);
			} 
			else if (IModificationFlags.DELETED == modificationFlag) {
				deletedList.add(step);
			} 
			else if (IModificationFlags.MODIFIED == modificationFlag) {
				
				long stepKey = routeStepDao.getStepKeyByUUID(innerData.getUuid());
				innerData.setId(stepKey);
				innerData.setRouteKey(routeEntity.getId());
				innerData.setLastModifiedTime(TimeUtil.getLocalDateTime());
				innerData.setLastModifier(SpringWebUtils.getRequestUser());
				innerData.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//				innerData.setLastModifiedPid(DataHandlerUtils.getDmomPid());
				
				step.setInnerData(innerData);
				modifiedList.add(step);
			}
		}

		if (createdList.size() > 0) {
			routeStepDao.saveRouteSteps(toInnerData(createdList));
			routeStepWCRelDataHandler.saveRelation(route.getInnerData().getId(),createdList);
		} 
		
		if (deletedList.size() > 0) {

			removeRelatedArc(route, deletedList);
			routeStepWCRelDataHandler.removeRelation(deletedList);
			routeStepDao.removeRouteStepsById(toInnerData(deletedList));
			route.removeRouteStep(deletedList);
		} 
		if (modifiedList.size() > 0) {

			routeStepWCRelDataHandler.removeRelation(modifiedList);
			routeStepDao.updateRouteSteps(toInnerData(modifiedList));
			routeStepWCRelDataHandler.saveRelation(route.getInnerData().getId(), modifiedList);
		}
		return stepList;
	}

	protected void removeStepsByRouteId(long routeId) {

		if (routeId <= 0) {
			return;
		}
		routeStepDao.removeStepsByRouteId(routeId);
	}

	public void removeRelatedArc(Route route, List<RouteStep> stepList) {

		List<Arc> arcList = new ArrayList<Arc>();

		for (RouteStep step : stepList) {
			arcList.addAll(getIncommingArcs(route, step));
		}
		
		arcDataHandler.removeArcs(arcList);
		route.removeRouteArc(arcList);
	}
	
	public List<RouteStep> getNextRouteSteps(long currentStepKey) {

		List<RouteStep> stepList = new ArrayList<RouteStep>();

		List<Long> stepKeyList = arcDataHandler.getNextRouteStepKeys(currentStepKey);
		List<RouteStepEntity> entityList = routeStepDao.getNextRouteSteps(stepKeyList);

		for (RouteStepEntity stepEntity : entityList) {

			RouteStep routeStep = new RouteStep();
			routeStep.setInnerData(stepEntity);

			stepList.add(routeStep);
		}

		return stepList;

	}

	public List<RouteStep> getRouteStepsByRouteId(long routeId) {

		List<RouteStep> stepList = new ArrayList<RouteStep>();

		List<RouteStepEntity> entityList = routeStepDao.getRouteStepsByRouteId(routeId);
		for (RouteStepEntity stepEntit : entityList) {

			RouteStep beanObj = new RouteStep();
			beanObj.setInnerData(stepEntit);

			stepList.add(beanObj);
		}
		
		return stepList;
	}

	public List<Arc> getIncommingArcs(Route route, RouteStep routeStep) {

		List<Arc> incommingArs = new ArrayList<Arc>();

		List<Arc> allArcs = route.getRouteArcs();
		for (Arc arc : allArcs) {
			if (arc.getInnerData().getToNodeUUID() == routeStep.getInnerData().getUuid()) {
				incommingArs.add(arc);
			}
		}
		
		return incommingArs;
	}

	protected List<RouteStep> fromInnerData(List<RouteStepEntity> entityList) {

		List<RouteStep> stepList = new ArrayList<RouteStep>();

		for (RouteStepEntity stepEntity : entityList) {

			RouteStep stepBean = new RouteStep();
			stepBean.setInnerData(stepEntity);

			stepList.add(stepBean);
		}
		return stepList;
	}

	protected List<RouteStepEntity> toInnerData(List<RouteStep> stepList) {

		List<RouteStepEntity> entityList = new ArrayList<RouteStepEntity>();

		for (RouteStep step : stepList) {

			entityList.add(step.getInnerData());
		}
		return entityList;
	}
	
	public int getRoueStepByOperationId(long operationId) {
		return routeStepDao.getRoueStepByOperationId(operationId);
	}
	
	public List<WorkCenter> getAttachedWorkCenterList(long stepKey) {
		
		List<WorkCenter> wcList = new ArrayList<WorkCenter>();
		
		
		
		return wcList;
	}

	public RouteStep getRouteStepByName(long routeKey, String stepName) {

		RouteStepEntity stepEntity = routeStepDao.getRouteStepByName(routeKey, stepName);

		if (stepEntity == null) {
			return null;
		}

		RouteStep routeStep = new RouteStep();
		routeStep.setInnerData(stepEntity);

		return routeStep;
	}
	
	public RouteStep getRouteStepByKey(long stepKey) {

		RouteStep routeStep = new RouteStep();

		RouteStepEntity stepEntity = routeStepDao.getRouteStepByKey(stepKey);
		if (stepEntity != null) {
			routeStep.setInnerData(stepEntity);
			return routeStep;
		}

		return null;
	}
}
